import config from "@/config"
import {
	getToken
} from "@/utils/auth.js"

/**
 * 显示消息提示框
 * @param content 提示的标题
 */
function toast(content) {
	uni.showToast({
		icon: 'none',
		title: content,
		position: 'bottom'
	})
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
function showConfirm(content) {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title: '提示',
			content: content,
			cancelText: '取消',
			confirmText: '确定',
			success: function(res) {
				resolve(res)
			}
		})
	})
}
/**
 * 手机号隐藏中键四位
 * @param {Object} params
 */
function starPhone(phone) {
	if (!phone) {
		return
	}
	return phone.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2')
}

/**
 * 参数处理
 * @param params 参数
 */
function tansParams(params) {
	let result = ''
	for (const propName of Object.keys(params)) {
		const value = params[propName]
		var part = encodeURIComponent(propName) + "="
		if (value !== null && value !== "" && typeof(value) !== "undefined") {
			if (typeof value === 'object') {
				for (const key of Object.keys(value)) {
					if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
						let params = propName + '[' + key + ']'
						var subPart = encodeURIComponent(params) + "="
						result += subPart + encodeURIComponent(value[key]) + "&"
					}
				}
			} else {
				result += part + encodeURIComponent(value) + "&"
			}
		}
	}
	return result
}


function uploadFilePromise(url) {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: config.baseUrl + '/common/uploadOSS', // 仅为示例，非真实的接口地址
			filePath: url,
			name: 'file',
			header: {
				'content-type': 'multipart/form-data',
				'Authorization': getToken(),
			},
			success: (resp) => {
				resolve(resp.data);
			},
		})
	})
}

export function callPhone(phone) {
	uni.makePhoneCall({
		phoneNumber: phone // 电话号码
	});
}


/**
 * @param {Object} obj
 * 判断对象是否为空
 */
function isEmptyObject(obj) {
	return Object.keys(obj).length === 0 && obj.constructor === Object;
}


export default {
	toast,
	tansParams,
	isEmptyObject,
	showConfirm
}